package com.huang.slideswitch.SlideSwitchView;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * Created by huangRZ on 2016/7/30.
 */
public class SlideSwitchView extends View {

    private String NAME_PLACE = "http://schemas.android.com/apk/res-auto";
    private Context context;
    private boolean switch_state;
    private Bitmap switchBackgroundBitmap;
    private Bitmap slideButtonBitmap;
    private int currentX;
    private Paint paint;

    /**
     * 直接new出来该view对象时调用该构造方法
     *
     * @param context
     */
    public SlideSwitchView(Context context) {
        super(context);
        this.context = context;
    }

    /**
     * 在xml文件中定义该view对象，添加属性但是不添加样式时调用该构造方法
     *
     * @param context
     * @param attrs
     */
    public SlideSwitchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        //获取slideSwitchView的三个属性的值
        switch_state = attrs.getAttributeBooleanValue(NAME_PLACE, "switch_state", false);
        int switch_background = attrs.getAttributeResourceValue(NAME_PLACE, "switch_background", -1);
        int slide_button = attrs.getAttributeResourceValue(NAME_PLACE, "slide_button", -1);

        initView(switch_background, slide_button);
    }

    /**
     * 在xml文件中定义该view对象，添加属性并且添加样式时调用该构造方法
     *
     * @param context      调用该view对象的上下文
     * @param attrs        属性值集合
     * @param defStyleAttr 定义的样式
     */
    public SlideSwitchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        //获取slideSwitchView的三个属性的值
        switch_state = attrs.getAttributeBooleanValue(NAME_PLACE, "switch_state", false);
        int switch_background = attrs.getAttributeResourceValue(NAME_PLACE, "switch_background", -1);
        int slide_button = attrs.getAttributeResourceValue(NAME_PLACE, "slide_button", -1);

        initView(switch_background, slide_button);
    }

    /**
     * 从属性值给出的资源id，构建组件背景图片
     */
    private void initView(int switch_background, int slide_button) {
        paint = new Paint();
        switchBackgroundBitmap = BitmapFactory.decodeResource(getResources(), switch_background);
        slideButtonBitmap = BitmapFactory.decodeResource(getResources(), slide_button);
    }

    /**
     * 1、首先对控件进行测量
     *
     * @param widthMeasureSpec  父类指定的宽
     * @param heightMeasureSpec 父类指定的高
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //必须在该方法中调用，参数为该view对象的宽高
        setMeasuredDimension(switchBackgroundBitmap.getWidth(), switchBackgroundBitmap.getHeight());
    }

    /**
     * 2、然后对控件进行绘制
     *
     * @param canvas 画布
     */
    @Override
    protected void onDraw(Canvas canvas) {
        //测量好宽高后，就有了一个矩形画布，左上角就是原点，参数中的0表示：在x和y轴上的偏移量
        //给控件设置背景图
        canvas.drawBitmap(switchBackgroundBitmap, 0, 0, paint);

        //计算滑块的移动范围
        int minLeft =  0;
        int maxLeft = switchBackgroundBitmap.getWidth() - slideButtonBitmap.getWidth();

        //判断当前是否为触摸移动事件
        if (touchMode) {
            //计算手指处于按钮中间时，按钮的坐标
            int newLeft = currentX - slideButtonBitmap.getWidth() / 2;
            //不让滑动滑出背景图片
            if (newLeft < minLeft){
                newLeft = minLeft;
            } else if (newLeft > maxLeft){
                newLeft = maxLeft;
            }
            canvas.drawBitmap(slideButtonBitmap, newLeft, 0, paint);
        } else {
            //根据开关状态值画出滑块的位置
            if (switch_state){
                canvas.drawBitmap(slideButtonBitmap, minLeft, 0, paint);
            } else {
                canvas.drawBitmap(slideButtonBitmap, maxLeft, 0, paint);
            }
        }
    }


    /**
     * 该控件的触摸事件
     *
     * @param event 触摸事件
     * @return true:自己处理该触摸事件，不传递下去；false：自己处理该触摸事件，并将触摸事件传递下去
     */
    boolean touchMode = false;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        switch (action) {
            case MotionEvent.ACTION_MOVE:
                touchMode = true;
                //获取手指移动时的x坐标
                currentX = (int) event.getX();
                break;
            case MotionEvent.ACTION_DOWN:
                touchMode = false;
                //获取手指按下的x坐标
                currentX = (int) event.getX();
                break;
            case MotionEvent.ACTION_UP:
                touchMode = false;
                //获取手指松开时的X坐标
                currentX = (int) event.getX();
                //获取控件的中间位置坐标
                int mediumX = switchBackgroundBitmap.getWidth() / 2;
                //判断手指在控件的左边还是右边
                if (currentX < mediumX){//在左边
                    switch_state = true;
                } else {    //在右边
                    switch_state = false;
                }
                break;

            default:
                break;
        }

        //重新绘制控件
        invalidate();

        return true;
    }

    /**
     * 3、该控件设置其他方法
     */


}
